import { from, Observable, of } from 'rxjs';
import { SubscribeMessage, WebSocketGateway, WebSocketServer, WsResponse, MessageBody, ConnectedSocket, OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect } from "@nestjs/websockets";
import { Server, Socket } from 'socket.io';
import { Param } from '@nestjs/common';
import { type } from 'os';

@WebSocketGateway({ transport: 'socket' })
export class EventsGateway implements OnGatewayInit, OnGatewayConnection, OnGatewayDisconnect {

    @WebSocketServer()
    server: Server;

    handleDisconnect(client: Socket) {
        console.log('websocket 断开连接',client.id)
    }
    handleConnection(client: Socket, ...args: any[]) {
        console.log('websocket 连接',args[0])
    }


    afterInit(server: Server) {
        // this.logger.log('chatGetway')
        console.log('websocket server 服务初始化')
    }



    @SubscribeMessage('events')
    handleEvent(@MessageBody() data: string, @ConnectedSocket() client: Socket): WsResponse<unknown> {
        // console.log(client)

        client.emit('events', { name: 'Nest' })
        client.send("{ event:'events',data:'hahaxxxh' }")

        return { event: 'events', data: 'hahah' };
    }


} 